package com.water.camera.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.GridLayoutManager;

import com.water.camera.adapter.BatchSuccAdapter;
import com.water.camera.bean.MediaData;
import com.water.camera.databinding.ActivityBatchSuccessBinding;
import com.water.camera.util.ToastUtil;

import java.io.File;
import java.util.ArrayList;

public class BatchSuccessAct extends BaseSaveAct {

    ActivityBatchSuccessBinding binding;
    ArrayList<String> img;
    ArrayList<MediaData> list;
    BatchSuccAdapter adapter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityBatchSuccessBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        binding.ivClose.setOnClickListener(v -> finish());
        img = getIntent().getStringArrayListExtra("output");
        list = getIntent().getParcelableArrayListExtra("img");
        binding.btnSave.setText(String.format("全部保存（%d张）", img.size()));
        binding.tvCount.setText(String.valueOf(img.size()));
        binding.rvBatch.setLayoutManager(new GridLayoutManager(this, 2));
        adapter = new BatchSuccAdapter(this, img);
        binding.rvBatch.setAdapter(adapter);
        binding.ivTipsClose.setOnClickListener(v -> binding.rlTips.setVisibility(View.GONE));
        binding.btnSave.setOnClickListener(v -> {
            save(img);
        });
    }

    @Override
    protected boolean paddingTop() {
        return false;
    }

    @Override
    protected void finishSave() {
        ToastUtil.showShortToast(this, "已保存");
        setResult(RESULT_OK);
        finish();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (img != null) {
            for (String s : img) {
                new File(s).delete();
            }
        }
    }

    public void toPreview(int p) {
        Intent i = new Intent();
        i.putExtras(getIntent());
        i.putStringArrayListExtra("output", img);
        i.putParcelableArrayListExtra("img", list);
        i.setClass(this, BatchResultPreviewAct.class);
        i.putExtra("position", p);
        startActivityForResult(i, 10);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 10) {
            if (resultCode == RESULT_OK) {
                img = data.getStringArrayListExtra("output");
                list = data.getParcelableArrayListExtra("img");
                adapter.update(img);
            }
        }
    }
}
